package com.hllxd.hism.star.basic.service.model.doris;

import lombok.Data;

import java.util.*;

/**
 * @author xiyong.lxy
 * @date 2020-11-1 20:37
 */
@Data
public class FkDapanEntity {
    public String dt;
    public String metaId;
    public String content;
    public String contentInc7;
    public String contentInc365;
    public String contentFmt;
    public String color;
    public static FkDapanEntity build(String dt, String metaId, String content, String contentInc7) {
        FkDapanEntity thiz = new FkDapanEntity();
        thiz.dt = dt;
        thiz.metaId = metaId;
        thiz.content = content;
        thiz.contentInc7 = contentInc7;
        return thiz;
    }

    public static Map<String,FkDapanEntity> buildList(List<FkDapanEntity> list) {

        // 需要针对FkDapan中的所有指标进行格式化
        // 针对指标依次处理
        // 核心指标（周日均-非串联数据）
        // UV日活	收入（黄金+优选）	曝光AS	VPPV详情页浏览量	核心连接（投递）	核心连接（微聊）
        // all^uv, 58^uv, ganji^uv
        // all^cost, 58^cost, ganji^cost
        // all^exp_as all^exp_uv, 58^exp_as 58^exp_uv, ganji^exp_as ganji^exp_uv
        // all^app_click_as all^app_click_uv, 58^app_click_as 58^app_click_uv, ganji^app_click_as ganji^app_click_uv
        // all^delivery_as all^delivery_uv, 58^delivery_as 58^delivery_uv, ganji^delivery_as ganji^delivery_uv
        // all^im_as all^im_uv, 58^im_as 58^im_uv, ganji^im_as ganji^im_uv

        // 赶集直招新增：6.64w(-9.11%)  新增次日留存率：21.53%(4.62%)
        // ganji^new_user, ganji^ren_user
        // 活跃招聘者:55.42w(-1.97%), 对比阳历年-31.74%，对比阴历年-29.97%
        // all^active_buv, all^active_buv_lunar, all^active_buv_solar
        // 获得投递活跃招聘者：15.19w(-2.61%)，活跃招聘者获得总投递量：43.75w(-4.52%)
        // all^buv_hytd, all^cnt_hytd
        // 活跃职位数:435.02w(-9.97%), 对比阳历年-48.35%，对比阴历年-43.25%
        // all^active_cate, all^active_cate_lunar, all^active_cate_solar

        // 整个处理，仅仅 all^active_buv_lunar， all^active_buv_solar， 以及 all^active_cate_lunar, all^active_cate_solar 需要特殊处理

        // 定义key以及refer相关联的处理
        Map<String,String> metaMap = new HashMap<>(6);
        metaMap.put("all^active_buv",null);
        metaMap.put("all^active_buv_lunar","all^active_buv");
        metaMap.put("all^active_buv_solar","all^active_buv");
        metaMap.put("all^active_cate",null);
        metaMap.put("all^active_cate_lunar","all^active_cate");
        metaMap.put("all^active_cate_solar","all^active_cate");

        Map<String,FkDapanEntity> mapFkDapanEntity = new HashMap<>(list.size());
        for(FkDapanEntity item:list) {
            if(item==null||item.content==null) {
                continue;
            }
            double value = Double.parseDouble(item.content);
            double inc7 = Double.parseDouble(item.contentInc7);
            item.color = (inc7>=0)?(inc7==0)?"grey":"red":"green";
            if(value<=1) {
                item.contentFmt = String.format("%.02f%%(%s)", value*100, diffRatioFmt(inc7));
            }
            else if(value>1 && value<=10000) {
                item.contentFmt = String.format("%.02f(%s)", value, diffRatioFmt(inc7));
            } else if(value>10000 && value<=100000000) {
                // 转换为万
                item.contentFmt = String.format("%.02fw(%s)", value/10000, diffRatioFmt(inc7));
            }else {
                // 转换为亿
                item.contentFmt = String.format("%.02fy(%s)", value/100000000, diffRatioFmt(inc7));
            }
            mapFkDapanEntity.put(item.metaId, item);
        }
        // 特殊的需要处理
        for(Map.Entry<String,String> entry: metaMap.entrySet()) {
            String curr = entry.getKey();
            String refer = entry.getValue();
            if(refer==null) {
                // 不需要处理指标
                continue;
            }
            FkDapanEntity referEntity = mapFkDapanEntity.get(refer);
            FkDapanEntity item = mapFkDapanEntity.get(curr);
            if(referEntity==null || item==null) {
                continue;
            }
            double currVal = Double.parseDouble(item.content);
            double referVal = Double.parseDouble(referEntity.content);
            // currVal 对比值， referVal 当前值
            double diff = (referVal-currVal)/currVal;
            item.color = (diff>=0)?(diff==0)?"grey":"red":"green";
            item.contentFmt = diffRatioFmt(diff);
        }
        return mapFkDapanEntity;
    }
    public static String diffRatioFmt(double diff) {
        if(diff>=0.0) {
            // 增加+号
            return String.format("+%.2f%%", diff * 100);
        }else {
            return String.format("%.2f%%", diff * 100);
        }
    }


    public static Map<String,FkDapanEntity> buildList2(List<FkDapanEntity> list) {

        // 需要针对FkDapan中的所有指标进行格式化

        Map<String,FkDapanEntity> mapFkDapanEntity = new HashMap<>(list.size());
        for(FkDapanEntity item:list) {
            if(item==null||item.content==null) {
                continue;
            }
            double value = Double.parseDouble(item.content);
            double contentInc7 = Double.parseDouble(item.contentInc7);
            double inc7 = (value-contentInc7)/value;

            item.color = (inc7>=0)?(inc7==0)?"grey":"red":"green";
            if(value<=1) {
                item.contentFmt = String.format("%.02f%%(%s)", value*100, diffRatioFmt(inc7));
            }
            else if(value>1 && value<=10000) {
                item.contentFmt = String.format("%.02f(%s)", value, diffRatioFmt(inc7));
            } else if(value>10000 && value<=100000000) {
                // 转换为万
                item.contentFmt = String.format("%.02fw(%s)", value/10000, diffRatioFmt(inc7));
            }else {
                // 转换为亿
                item.contentFmt = String.format("%.02fy(%s)", value/100000000, diffRatioFmt(inc7));
            }
            mapFkDapanEntity.put(item.metaId, item);
        }
        return mapFkDapanEntity;
    }
}
